package org.yanghaokun.order.service.impl;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.yanghaokun.basic.service.impl.BaseServiceImpl;
import org.yanghaokun.basic.util.CodeGenerateUtils;
import org.yanghaokun.basic.util.DistanceUtil;
import org.yanghaokun.order.domain.OrderAddress;
import org.yanghaokun.order.domain.ProductOrder;
import org.yanghaokun.order.mapper.OrderAddressMapper;
import org.yanghaokun.order.mapper.ProductOrderMapper;
import org.yanghaokun.order.service.IProductOrderService;
import org.yanghaokun.org.domain.Shop;
import org.yanghaokun.org.mapper.ShopMapper;
import org.yanghaokun.pay.constants.PayConstants;
import org.yanghaokun.product.domain.Product;
import org.yanghaokun.product.mapper.ProductMapper;
import org.yanghaokun.user.domain.LoginInfo;
import org.yanghaokun.user.domain.User;
import org.yanghaokun.user.domain.UserAddress;
import org.yanghaokun.user.mapper.UserAddressMapper;
import org.yanghaokun.user.mapper.UserMapper;

import java.math.BigDecimal;
import java.util.Date;
import java.util.Map;

@Service
public class ProductOrderServiceImpl extends BaseServiceImpl<ProductOrder> implements IProductOrderService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private ProductOrderMapper productOrderMapper;
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private UserAddressMapper userAddressMapper;
    @Autowired
    private OrderAddressMapper orderAddressMapper;
    @Autowired
    ShopMapper shopMapper;

    @Override
    public String submit(Map<String, Object> params, LoginInfo loginInfo) {
        //前台传过来的四个参数，我们需要从对象中一一取出
        Long addressId = Long.valueOf(params.get("address_id").toString());
        Integer serviceMethod = Integer.valueOf(params.get("service_method").toString());
        Integer payMethod = Integer.valueOf(params.get("pay_method").toString());
        Long productId = Long.valueOf(params.get("product_id").toString());
        Long productNum = Long.valueOf(params.get("product_num").toString());
        //和用户进行绑定
        Product product = productMapper.findOne(productId);
        product.setSalecount(product.getSalecount()+productNum);
        productMapper.update(product);
        User user = userMapper.findByloginId(loginInfo.getId());
        UserAddress userAddress = userAddressMapper.findOne(addressId);
        //创建服务订单
        ProductOrder productOrder = initPrdouctOrder(payMethod, productId, productNum, product, user, userAddress);
        //绑定了之后更新一下服务订单
        productOrderMapper.save(productOrder);
        //配送方式的添加 @TODO
        //创建订单的地址
        //根据用户的地址来转化为订单地址
        OrderAddress orderAddress = userAddress2OrderAddress(userAddress, productOrder);
        //保存订单
        orderAddressMapper.save(orderAddress);
        //创建支付订单斌发起支付请求 @TODO
        return null;
    }

    private ProductOrder initPrdouctOrder(Integer payMethod, Long productId, Long productNum, Product product, User user,UserAddress userAddress) {
        ProductOrder productOrder = new ProductOrder();
        productOrder.setUser_id(user.getId());
        productOrder.setDigest("[摘要]服务"+product.getName()+"的订单！");
        productOrder.setState(0);
        productOrder.setPrice(product.getSaleprice()
                .multiply(new BigDecimal(productNum))); //价格=服务单价*数量

        productOrder.setOrderSn(CodeGenerateUtils.generateOrderSn(user.getId()));
        productOrder.setPaySn(payMethod);
        productOrder.setLastPayTime(new Date(System.currentTimeMillis()+ PayConstants.ORDER_TIMEOUT));
        productOrder.setProduct_id(productId);

        //获取最近店铺并完成绑定的
        Shop nearestShop = DistanceUtil.getNearestShop(DistanceUtil.getPoint(userAddress.getFullAddress()), shopMapper.findAll());
        if (nearestShop!=null) {
            productOrder.setShop_id(nearestShop.getId());
        }

        return productOrder;
    }

    private OrderAddress userAddress2OrderAddress(UserAddress userAddress, ProductOrder productOrder) {
        OrderAddress orderAddress = new OrderAddress();
        BeanUtils.copyProperties(userAddress,orderAddress);
        orderAddress.setId(null);
        orderAddress.setOrder_id(productOrder.getId());
        orderAddress.setOrderSn(productOrder.getOrderSn());
        return orderAddress;
    }

    @Override
    public void cancelOrder(Long orderId) {

    }
}
